
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{
    public partial class NewsHouseBOList : Csla.BusinessListBase<NewsHouseBOList, NewsHouseBO>
    {

        #region Authorization Rules

        protected static void AddObjectAuthorizationRules()
        {
            //TODO: Define object-level authorization rules of NewsHouseBOList
            //AuthorizationRules.AllowCreate(typeof(NewsHouseBOList), "NewsHouseBOListCreateGroup"));
            //AuthorizationRules.AllowEdit(typeof(NewsHouseBOList), "NewsHouseBOListEditGroup"));
            //AuthorizationRules.AllowDelete(typeof(NewsHouseBOList), "NewsHouseBOListDeleteGroup"));
        }
        #endregion //Authorization Ruless
        #region Trang Loai Nha Dat - Kiem tra tin nha dat con ton tai khi xoa loai nha dat
            public static bool CheckNewsHouseinAreaDetail(int? areaDetailID, string username, int? categoryid)
            {
                int count = 0;
                using (var mgr = ContextManager<HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
                {
                    if (areaDetailID.HasValue)
                        count = mgr.DataContext.NewsHouses.Count(n => n.AreaDetailID == areaDetailID);
                    else
                    {
                        if (username != "")
                            count = mgr.DataContext.NewsHouses.Count(u => u.UserName == username);
                        else
                            count = mgr.DataContext.NewsHouses.Count(c => c.CategoryID == categoryid);
                    }
                }

                return (count > 0);
            }
        #endregion //Trang Loai Nha Dat - Kiem tra tin nha dat con ton tai khi xoa loai nha dat

       #region Trang Loai tien te - Kiem tra nhung tin nha dat con ton tai khi xoa loai tien te
            public static bool CheckCurrencyidInNewsHouse(int currencyid)
        {  
             int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.NewsHouses.Count(n => n.CurrencyID == currencyid);
            }

            return (count > 0);
        }
       #endregion //Trang Loai tien te - Kiem tra nhung tin nha dat con ton tai khi xoa loai tien te

            #region Trang Danh muc

            #region Ham kiem tra tin con ton tai trong danh muc nha dat can xoa
            public static bool CheckCategoryInNewsHouse(int cateID)
            {
                int count = 0;
                using (var mgr = ContextManager<HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
                {
                    count = mgr.DataContext.NewsHouses.Count(n => n.CategoryID == cateID);
                }

                return (count > 0);
            }
            #endregion // end kiem tra khi xoa danh muc

            #endregion //Trang Danh muc

        #region Trang Tin Nha Dat
        #region ham lay tat ca danh sach tin nha dat
        public static NewsHouseBOList GetAllNewsHousesBOList()
        {
            return DataPortal.Fetch<NewsHouseBOList>();
        }
        #endregion

        #region truy cap du lieu
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch()
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.NewsHouses
                    orderby row.PostDate descending
                    select NewsHouseBO.GetNewsHouseBO(row.NewsHouseID)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion
    }
}
